package com.heima.behavior.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.heima.behavior.mapper.ApArticleLikeMapper;
import com.heima.behavior.mapper.ApArticleUnlikeMapper;
import com.heima.behavior.mapper.ApBehaviorMapper;
import com.heima.behavior.service.ApBehaviorService;
import com.heima.model.article.pojos.ApArticle;
import com.heima.model.behavior.dtos.ApArticleCountDto;
import com.heima.model.behavior.dtos.IsLikeDto;
import com.heima.model.behavior.dtos.IsUnlikeDto;
import com.heima.model.behavior.dtos.LikesBehaviorDto;
import com.heima.model.behavior.dtos.UnLikesBehaviorDto;
import com.heima.model.behavior.pojos.ApArticleCount;
import com.heima.model.behavior.pojos.ApArticleLike;
import com.heima.model.behavior.pojos.ApArticleUnlike;

import com.heima.model.behavior.pojos.ApArticleUnlike;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.time.LocalDate;


@Slf4j
@Service
public class ApBehaviorServiceImpl implements ApBehaviorService {
    @Autowired
    private ApBehaviorMapper apBehaviorMapper;


    @Autowired
    private ApArticleLikeMapper apArticleLikeMapper;

    @Autowired
    private ApArticleUnlikeMapper apArticleUnlikeMapper;

    /**
     * 点赞行为
     *
     * @param dto
     */
    @Override
    public void likeBehavior(LikesBehaviorDto dto) {
        log.info("点赞行为：{}", dto);
        ApArticleLike apArticleLike = new ApArticleLike();
        //判断是否已经点赞
        ApArticleLike articleLike = apBehaviorMapper.findLikeByArticleId(dto.getArticleId());
        if (!StringUtils.isEmpty(articleLike)) {
            if (articleLike.getOperation().intValue() == 0) {
                apArticleLike.setOperation((short) 1);
                apBehaviorMapper.updateCancelLike(dto.getArticleId());
            }else if (articleLike.getOperation().intValue() == 1) {
                apArticleLike.setOperation((short) 0);
                apBehaviorMapper.likeBehavior(dto.getArticleId());
            }
            return;
        }
        ApArticle apArticle = apBehaviorMapper.findArticle(dto.getArticleId());
        apArticleLike.setLikeTime(LocalDate.now());
        apArticleLike.setArticleId(dto.getArticleId().toString());
        apArticleLike.setType(dto.getType());
        apArticleLike.setAuthorId(apArticle.getAuthorId());
        apArticleLike.setOperation(dto.getOperation());
        apArticleLike.setUserId(4L);
        apBehaviorMapper.insertArticleLike(apArticleLike);

    }

    /**
     * 阅读次数
     *
     * @param dto
     */
    @Override
    public void counRehavior(ApArticleCountDto dto) {
        log.info("阅读次数：{}", dto);
        //根据文章id获取阅读次数表信息
        ApArticleCount apArticleCount = apBehaviorMapper.findReadCounts(dto.getArticleId().toString());
        if (!StringUtils.isEmpty(apArticleCount)) {
            apArticleCount.setCount(apArticleCount.getCount() + 1);
            apBehaviorMapper.updateReadBehaviorCounts(apArticleCount.getArticleId(), apArticleCount.getCount());
        } else {
            ApArticleCount articleCount = new ApArticleCount();
            articleCount.setArticleId(dto.getArticleId().toString());
            articleCount.setCount(dto.getCount());
            apBehaviorMapper.insertArticleCounts(articleCount.getArticleId(), articleCount.getCount());
        }
    }

    /**
     * 不喜欢
     *
     * @param dto
     */
    @Override
    public void unlikesBehavior(UnLikesBehaviorDto dto) {
        log.info("不喜欢：{}", dto);
        ApArticleUnlike apArticleUnlike = new ApArticleUnlike();
        ApArticleUnlike unlike = apBehaviorMapper.findUnLikeByArticleId(dto.getArticleId());
        if (!StringUtils.isEmpty(unlike)) {
            if (unlike.getType().intValue() == 0) {
                unlike.setType((short) 1);
                apBehaviorMapper.updateUnlikeBehavior(unlike.getArticleId(), unlike.getType());
            } else if (unlike.getType().intValue() == 1) {
                unlike.setType((short) 0);
                apBehaviorMapper.updateUnlikeBehavior(unlike.getArticleId(), unlike.getType());
            }
            return;
        }
        apArticleUnlike.setArticleId(dto.getArticleId().toString());
        apArticleUnlike.setType(dto.getType());
        apArticleUnlike.setUserId(4L);
        apBehaviorMapper.addUnlikesBehavior(apArticleUnlike.getUserId(),apArticleUnlike.getArticleId(),apArticleUnlike.getType());
    }

    @Override
    public Boolean isLike(IsLikeDto isLikeDto) {
        LambdaQueryWrapper<ApArticleLike> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ApArticleLike::getUserId, isLikeDto.getUserId())
                .eq(ApArticleLike::getArticleId, isLikeDto.getArticleId());
        ApArticleLike articleLike = apArticleLikeMapper.selectOne(wrapper);
        return articleLike != null;
    }

    @Override
    public Boolean isUnlike(IsUnlikeDto isUnlikeDto) {
        LambdaQueryWrapper<ApArticleUnlike> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ApArticleUnlike::getUserId, isUnlikeDto.getUserId())
                .eq(ApArticleUnlike::getArticleId, isUnlikeDto.getArticleId());

        ApArticleUnlike apArticleUnlike = apArticleUnlikeMapper.selectOne(wrapper);
        return apArticleUnlike != null;
    }
}
